package JUC.callable;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * @author mubei
 * @date 2021/11/21
 * @description
 */
public class TestCallable {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //new Thread(new Runnable()).start()
        //new Thread(new FutureTask<>(启动Callable))


        new Thread(new FutureTask<String>(new MyThread()),"A").start();


        // callable 的实现类
        MyThread myThread = new MyThread();
        // FutureTask 是 Runnable的实现类 沟通了 Callable和Thread
        FutureTask<String> task = new FutureTask<String>(myThread);

        // 装入
        new Thread(task,"B").start();
        // 获取返回值  get可能阻塞   放在最后一行
        String s = task.get();
        System.out.println(s);

    }
}

class MyThread implements Callable<String> {
    @Override
    public String call() throws Exception {
        System.out.println("123");
        return "123";
    }
}
